package com.ayz.gateway.core.session.convert;



import com.ayz.gateway.common.convert.support.GenericConverter;
import com.ayz.gateway.common.type.SimpleTypeRegistry;
import com.ayz.gateway.core.datasource.RpcStatement;
import com.ayz.gateway.core.rpc.MappedMethod;
import com.ayz.gateway.core.session.http.HttpStatement;

import java.util.Map;

/**
 * @ClassName HttpStatToRpcStatConvert
 * @Description TODO: 将HttpStatement类型和MappedMethod类型 组合 最终返回RpcStatement
 * @Author 快乐的星球
 * @Date 2023/5/10 14:26
 * @Version 1.0
 **/
public class HttpStatToRpcStatConvert implements GenericConverter<HttpStatement, MappedMethod, RpcStatement> {
    @Override
    public RpcStatement convert(HttpStatement httpStatement, MappedMethod mappedMethod) {
        RpcStatement rpcStatement=new RpcStatement();
        //1:服务名称
        rpcStatement.setApplicationName(mappedMethod.getApplicationName());
        //2:接口名称
        rpcStatement.setInterfaceName(mappedMethod.getInterfaceName());
        //3:方法名称
        rpcStatement.setMethodName(mappedMethod.getMethodName());
        //4: Rpc同步调用还是异步调用
        rpcStatement.setSync(mappedMethod.isSync());
        //5: 响应回调函数
        rpcStatement.setResponseCallback(httpStatement.getResponseCallback());
        //6: 方法参数全类名数组
        rpcStatement.setParameterTypeNames(mappedMethod.getParameterTypeNames());
        //7:方法参数
        Map<String, Object> requestPrams =httpStatement.getRequestPrams();
        Object[] args= SimpleTypeRegistry.isSimpleType(mappedMethod.getParameterTypeNames()[0])?requestPrams.values().toArray() : new Object[]{requestPrams};

        rpcStatement.setArgs(args);
        return rpcStatement;
    }
}
